package org.setaria.control
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.setaria.event.TabBarEvent;
	import org.setaria.skins.control.SetariaTabBarSkin;
	
	import spark.components.Button;
	import spark.components.Group;
	import spark.components.TabBar;
	import spark.events.RendererExistenceEvent;
	
	[Event(name="clickCloseButton", type="org.setaria.event.TabBarEvent")]
	public class SetariaTabBar extends TabBar
	{
		
		private var isLeft:Boolean = false;
		
		[SkinPart(required="false")]
		public var leftBtn:Button;
		[SkinPart(required="false")]
		public var rightBtn:Button;
		[SkinPart(required="false")]
		public var group:Group;
		
		public var controlButtonWidth:int = 14;
		
		/**
		 * 构造函数
		 * */
		public function SetariaTabBar()
		{
			super();
//			setStyle("skinClass",org.setaria.skins.control.SetariaTabBarSkin);
		}
		/**
		 * TabBarEvent.EVENT_CLICK_CLOSE_BUTTON 事件的处理函数
		 * 转发ItemButton的close事假
		 * */
		protected function ItemCloseButtonClickHandler(event:TabBarEvent):void
		{
			dispatchEvent(new TabBarEvent(event.type,event.data,event.index));
		}
		/**
		 * RendererExistenceEvent.RENDERER_ADD 事件的处理函数
		 * 让新加的Item处于选中状态，并为它注册 TabBarEvent.EVENT_CLICK_CLOSE_BUTTON 监听
		 * */
		protected function rendererAddHandler(event:RendererExistenceEvent):void
		{
			this.selectedIndex = event.index;
			ButtonBarButton(event.renderer).addEventListener(TabBarEvent.EVENT_CLICK_CLOSE_BUTTON,ItemCloseButtonClickHandler);
		}
		/**
		 * RendererExistenceEvent.RENDERER_REMOVE 事件的处理函数
		 * 
		 * */
		protected function rendererRemoveHandler(event:RendererExistenceEvent):void
		{
			ButtonBarButton(event.renderer).removeEventListener(TabBarEvent.EVENT_CLICK_CLOSE_BUTTON,ItemCloseButtonClickHandler);
		}
		/**
		 *  @private
		 */
		override protected function partAdded(partName:String, instance:Object):void{
			super.partAdded(partName,instance);
			if (instance == dataGroup){
				dataGroup.addEventListener(RendererExistenceEvent.RENDERER_ADD,rendererAddHandler);
				dataGroup.addEventListener(RendererExistenceEvent.RENDERER_REMOVE,rendererRemoveHandler);
			}else if (instance == leftBtn)
			{
				leftBtn.width = controlButtonWidth;
				leftBtn.addEventListener(MouseEvent.CLICK,mouseClickEventHandler);
				leftBtn.addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
				leftBtn.addEventListener(MouseEvent.MOUSE_UP,mouseEventHandler);
			}else if(instance == rightBtn){
				rightBtn.width = controlButtonWidth;
				rightBtn.addEventListener(MouseEvent.CLICK,mouseClickEventHandler);
				rightBtn.addEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
				rightBtn.addEventListener(MouseEvent.MOUSE_UP,mouseEventHandler);
			}
		}
		/**
		 *  @private
		 */
		override protected function partRemoved(partName:String, instance:Object):void
		{
			if (instance == dataGroup){
				dataGroup.removeEventListener(RendererExistenceEvent.RENDERER_ADD,rendererAddHandler);
				dataGroup.removeEventListener(RendererExistenceEvent.RENDERER_REMOVE,rendererRemoveHandler);
			}else if (instance == leftBtn)
			{
				leftBtn.removeEventListener(MouseEvent.CLICK,mouseClickEventHandler);
				leftBtn.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
				leftBtn.removeEventListener(MouseEvent.MOUSE_UP,mouseEventHandler);
			}else if(instance == rightBtn){
				rightBtn.removeEventListener(MouseEvent.CLICK,mouseClickEventHandler);
				rightBtn.removeEventListener(MouseEvent.MOUSE_DOWN,mouseEventHandler);
				rightBtn.removeEventListener(MouseEvent.MOUSE_UP,mouseEventHandler);
			}
			
			super.partRemoved(partName, instance);
		}
		/**
		 * 
		 * **/
		private function mouseClickEventHandler(event:MouseEvent):void{
			if(this.group.width> this.dataGroup.width && dataGroup.x>-1){
				return;
			}
			switch(event.target){
				case leftBtn :
					rightMoveMenu();	
					break;
				case rightBtn :
					leftMoveMenu();
					break;
			}
		}
		/**
		 * 
		 * **/
		private function mouseEventHandler(event:MouseEvent):void{
			if(this.group.width> this.dataGroup.width && dataGroup.x>-1){
				return;
			}
			switch(event.target){
				case leftBtn:
					isLeft = true;
					break;
				case rightBtn:
					isLeft = false;	
					break;
			}
			switch(event.type){
				case MouseEvent.MOUSE_DOWN:
					this.addEventListener(Event.ENTER_FRAME,enterFramHander);
					break;
				case MouseEvent.MOUSE_UP:
					this.removeEventListener(Event.ENTER_FRAME,enterFramHander);
					break;
			}
		}
		private function enterFramHander(event:Event):void{
			if(isLeft){
				rightMoveMenu();
			}else{
				leftMoveMenu();
			}
		}
		/**
		 * 往左移动一次
		 * **/
		private function leftMoveMenu():void{
			var tmpX:Number =  this.dataGroup.x-5;
			if(tmpX < (this.group.width - this.dataGroup.width)){
				this.dataGroup.x = this.group.width - this.dataGroup.width
			}else{
				this.dataGroup.x-=5
			}
		}
		/**
		 * 往右移动一次
		 * **/
		private function rightMoveMenu():void{
			var tmpX:Number =  this.dataGroup.x+5;
			if(tmpX > 0){
				this.dataGroup.x = 0
			}else{
				this.dataGroup.x+=5
			}
		}
	}
}